package com.client.common.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.client.common.service.SmsService;

import com.client.common.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Author 宋伟宁
 * @Date 2024/1/5
 * @Version 1.0
 **/
@Service
public class SmsServiceImpl implements SmsService {


    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    public boolean send(String phone, Map<String, Object> map) {
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI5tEuAxSuHHnqCpW8LfK2","VyM1I8aClzm2hSxJf6k1FIOroVXSOv");
        //发起 客户端
        IAcsClient client = new DefaultAcsClient(profile);
        //设置短信发送的信息 签名 手机号码 验证码
        CommonRequest request=new CommonRequest();
        //设置请求方式
        request.setSysMethod(MethodType.POST);
        //设置阿里云节点
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        //设置手机号码 设置成自己的
        request.putQueryParameter("PhoneNumbers",phone);
        //设置签名
        request.putQueryParameter("SignName","学习是一种生活习惯");
        //设置模板
        request.putQueryParameter("TemplateCode","SMS_180348176");
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));

        try {
            CommonResponse response=client.getCommonResponse(request);
            System.out.println(response);
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return false;
    }

    @Override
    public boolean send(String phone) {
        String code="";
        //随机6位数
        Random random = new Random();
        int randomNumber = random.nextInt(900000) + 100000;
        code=String.valueOf(randomNumber);

        Map<String,Object> map=new HashMap<>();
        map.put("code",code);
        boolean b=this.send(phone,map);
        if(b){
            //存入redis，有效期1分钟
            this.redisTemplate.opsForValue().set(phone+"_"+code, randomNumber+"",30*60, TimeUnit.MINUTES);
            return true;
        }else{
            return false;
        }
    }
}
